<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" pageOption />

    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">
    </ex_super_table_page>
    <ex_super_dialog_form ref="formDialog" :title="dialogTitle" v-bind="dialog" @afterSubmitSuccess="submitAfter">
    </ex_super_dialog_form>

  </div>
</template>
<script>

import superTablePageFunction from '@/mixin/super-table-page-function';
import page_table_model_manage from '@/mixin/page_table_model_manage';
import emitOnFunction from '@/mixin/emit-on-function';
const emitKey = 'wms>wave_strategy'
export default {
  title: 'label_wave_strategy',
  mixins: [superTablePageFunction, emitOnFunction(emitKey), page_table_model_manage],
  components: {},
  data() {
    return {
      selection: [],

      option: {
        search: {
          model: [
            { key: "strategyName", label: "label_strategy_name", type: 'select', enum: 'WavePlanRuleEnum', },
            { key: "strategyCode", label: "label_strategy_code", },
            { key: "status", label: "label_status", type: 'select', enum: 'IsAvailableStatusEnum', },
          ],
        },
        action: [
          {
            label: 'label_new_wave_strategy', attrs: { icon: "el-icon-plus" }, click: () => {
              this.dialogTitle = this.$tt('label_new_wave_strategy')
              this['$refs']['formDialog'].show()
            }
          },
        ],

        table: {
          url: "/wms-api/outbound/WaveStrategyRpc/selectListPage.json",
          model: this.init_table_model(),
        },
      },

      dialogTitle: '',
      dialog: {
        customClass: 'dialog_auto',
        submitUrl: '/wms-api/outbound/WaveStrategyRpc/saveUpdate.json',
        detailUrl: '/wms-api/outbound/WaveStrategyRpc/detail.json',
        detailDataHandle: (data) => {
          data.strategyList = data.strategyName.split(',')
          data.autoGenIntervalTimeNumber = data.autoGenIntervalTime
          data.autoGenIntervalTimeUnit = 'M'
          data.maximumWaitTimeNumber = data.maximumWaitTime
          data.maximumWaitTimeUnit = 'M'
          return data
        },
        submitDataHandle: (data) => {
          data.strategyName = data.strategyList.join(',')
          return data
        },
        form: {
          default: {
            priority: 1,
            strategyList: [],
            autoGenIntervalTime: 60,
            autoGenIntervalTimeNumber: 60,
            autoGenIntervalTimeUnit: 'M',
            maximumWaitTime: 60,
            maximumWaitTimeNumber: 60,

            maximumWaitTimeUnit: 'M',
            allowPdaTakeFlag: 'Y',
            capacityLower: 1,
            capacityMaximum: 99,
          },
          // itemClass: "widthdiv2form",
          itemClass: "form_item_small",
          inputClass: "form_input_default",
          formClass: "form_small_col2",
          inline: true,
          model: [
            { header: "label_strategy_info", },
            { key: 'strategyList', label: 'label_strategy_name', type: 'select', enum: 'WavePlanRuleEnum', multiple: true, },
            { key: 'strategyCode', label: 'label_strategy_code', disabled: true },
            {
              key: 'priority', label: 'label_priority', type: 'input-number',
              attrs: {
                'step-strictly': true, step: 1, min: 1,
              }
            },

            { header: "label_exec_condition", },

            {
              key: 'autoGenIntervalTime', label: 'label_auto_generate_interval_time',
              render: (h, { data, item }) => {
                return h('div', { class: 'flex_row' }, [
                  h('el-input-number', {
                    class: 'flex_cell',
                    props: {
                      value: data[item.key + 'Number'],
                      'step-strictly': true, step: 1, min: 1,

                    },
                    on: {
                      input: (e) => {
                        this.$set(data, item.key + 'Number', e)
                        if (data[item.key + 'Unit'] == 'H') {
                          this.$set(data, item.key, e * 60)
                        } else {
                          this.$set(data, item.key, e)
                        }
                      }
                    }
                  }),
                  h('ex_super_input', {
                    class: 'widthdiv3',
                    props: {
                      value: data[item.key + 'Unit'],
                      type: 'select',
                      attrs: {
                        items: [{ _value: 'M', _label: '分钟' }, { _value: 'H', _label: '小时' },]
                      },
                    },
                    on: {
                      input: (e) => {
                        let m = data[item.key]
                        if (e == 'H') {
                          m = m / 60
                        }
                        this.$set(data, item.key + 'Number', m)
                        this.$set(data, item.key + 'Unit', e)
                      }
                    }
                  }),
                ])
              }
            },
            {
              key: 'maximumWaitTime', label: 'label_max_waiting_time',
              render: (h, { data, item }) => {
                return h('div', { class: 'flex_row' }, [
                  h('el-input-number', {
                    class: 'flex_cell',
                    props: {
                      value: data[item.key + 'Number'],
                      'step-strictly': true, step: 1, min: 1,

                    },
                    on: {
                      input: (e) => {
                        this.$set(data, item.key + 'Number', e)
                        if (data[item.key + 'Unit'] == 'H') {
                          this.$set(data, item.key, e * 60)
                        } else {
                          this.$set(data, item.key, e)
                        }
                      }
                    }
                  }),
                  h('ex_super_input', {
                    class: 'widthdiv3',
                    props: {
                      value: data[item.key + 'Unit'],
                      type: 'select',
                      attrs: {
                        items: [{ _value: 'M', _label: '分钟' }, { _value: 'H', _label: '小时' },]
                      },
                    },
                    on: {
                      input: (e) => {
                        let m = data[item.key]
                        if (e == 'H') {
                          m = m / 60
                        }
                        this.$set(data, item.key + 'Number', m)
                        this.$set(data, item.key + 'Unit', e)
                      }
                    }
                  }),
                ])
              }
            },


            { key: 'capacityLower', label: 'label_capacity_min', type: 'input-number', attrs: { 'step-strictly': true, step: 1, min: 1, } },
            { key: 'capacityMaximum', label: 'label_capacity_max', type: 'input-number', attrs: { 'step-strictly': true, step: 1, min: 1, } },

            { key: 'allowPdaTakeFlag', label: 'label_allow_pda_collect_order', type: 'radio-group', enum: 'YesOrNoEnum' },
            { key: 'autoGenTaskFlag', label: 'label_auto_generate_task_order', type: 'radio-group', enum: 'YesOrNoEnum' },
        
          ],
          rules: {
            'strategyList': [{ required: true, type: 'array', message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'priority': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'autoGenIntervalTime': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            'maximumWaitTime': {
              required: true, trigger: 'blur',
              validator: (r, v, cb) => {
                if (_.isEmpty(v)) {
                  return cb(this.$tt('tip_can_no_empty'))
                } else if (v >= this.$refs.formDialog.$refs.form.getFormData().autoGenIntervalTime) {
                  return cb(this.$tt('tip_no_more_equal_any', { type: this.$tt('label_auto_generate_interval_time') }))
                }
                cb()
              }
            },
            'capacityLower': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'capacityMaximum': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'allowPdaTakeFlag': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'maxBoardNum': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'autoGenTaskFlag': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
          },
        }
      },
    };
  },
  methods: {
    option_table_model() {
      return [
        { type: 'selection', clickToggle: true, keyGroup: 'label_table_operate' },
        {
          type: 'expandDesc', 'width': 100,
          expandDesc: {
            class: "desc_flex_row",
            column: 4, border: true, direction: 'vertical', labelClassName: 'widthdiv4', contentClassName: 'widthdiv4',
            model: [],
          },
        },
        {
          key: 'strategyName', label: 'label_strategy_name', 'min-width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => v ? v.split(',').map(it => this.$enumShow(it, "WavePlanRuleEnum")).join(',') : '',
        },

        { key: 'strategyCode', label: 'label_strategy_code', 'min-width': 120, 'show-overflow-tooltip': true, },
        { key: 'priority', label: 'label_priority', 'width': 100, },

        { key: 'capacityLower', label: 'label_capacity_min', keyShow: "expandDesc", 'width': 100, 'show-overflow-tooltip': true, },
        { key: 'capacityMaximum', label: 'label_capacity_max', keyShow: "expandDesc", 'width': 100, 'show-overflow-tooltip': true, },

        { key: 'autoGenIntervalTime', label: 'label_auto_generate_interval_time', keyShow: "expandDesc", 'width': 120, 'show-overflow-tooltip': true, },
        { key: 'maximumWaitTime', label: 'label_max_waiting_time', keyShow: "expandDesc", 'width': 120, 'show-overflow-tooltip': true, },

        { key: 'allowPdaTakeFlag', label: 'label_allow_pda_collect_order', keyShow: "expandDesc", 'width': 100, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },
        { key: 'autoGenTaskFlag', label: 'label_auto_generate_task_order', keyShow: "expandDesc", 'width': 100, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },
        // { key: 'excludeSameOrderFlag', label: '排除同品订单数量', keyShow: "expandDesc", 'width': 100, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },
        // {
        //   key: 'varietyRestriction', label: '品种限定', keyShow: "expandDesc", 'width': 120, 'show-overflow-tooltip': true,
        //   formatter: (r, c, v, i) => this.$enumShow(v, "VarietyRestrictionEnum")
        // },
        { key: 'status', label: 'label_status', 'width': 100, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "IsAvailableStatusEnum") },

        { key: 'lastExecTime', label: 'label_last_exec_time', 'width': 130, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$dateFormat(v) },

        { key: 'creator', label: 'label_create_user', 'width': 100, keyShow: "expandDesc", 'show-overflow-tooltip': true, },
        { key: 'gmtCreate', label: 'label_create_time', 'width': 130, keyShow: "expandDesc", 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$dateFormat(v) },
        {
          type: 'operate', width: 210, operate: [
            {
              label: 'label_edit', click: (row) => {
                this.dialogTitle = this.$tt('label_edit_wave_strategy')
                this['$refs']['formDialog'].show({ "id": row["id"] })
              }
            },
            {
              label: "IsAvailableStatusEnum__ENABLE", type: 'confirm-button', url: '/wms-api/outbound/WaveStrategyRpc/updateStatus.json', message: 'label_sure_to_enable_this',
              if: row => !row.status || row.status == 'DISABLED',
              buttonClass: "button_text_success", confirmButtonType: 'success',
              params: (row) => { return { id: row.id, status: 'ENABLE' } }, done: ['reload', 'tip']
            },
            {
              label: "IsAvailableStatusEnum__DISABLED", type: 'confirm-button', url: '/wms-api/outbound/WaveStrategyRpc/updateStatus.json', message: 'label_sure_to_disable_this',
              if: row => row.status == 'ENABLE',
              buttonClass: "button_text_danger", confirmButtonType: 'danger',
              params: (row) => { return { id: row.id, status: 'DISABLED' } }, done: ['reload', 'tip']
            }
          ]
        },
      ]
    },
    submitAfter() {
      this.reload()
    },

  }
};

</script>
<style></style>
